const getBrowser = require('../browser/init');
const DBOP = require('../dao/book');

main();

async function main() {
  const browser = await getBrowser();
  const page = await browser.newPage();
  const detailUrl = await DBOP.getAllBook();
  // const detailUrl = await DBOP.getAllBookAndReadUrl_Null();
  // await getBookData(detailUrl[6286].detailUrl, page).catch(err => {
  //   console.log(err);
  // });
  for (let i = 4336; i < detailUrl.length; i++) {
    const url = detailUrl[i].detailUrl;
    try {
      await getBookData(url, page);
      // await updateReadUrl(page, detailUrl[i].bookName, url);
    } catch (error) {
      console.log(`${error}--第${i}本书失败---${url}`);
      continue;
    }
  }
}

// 获取书籍详情主页
async function getDetail(detailUrl, page) {
  await page.goto(detailUrl);
  // 书籍名称
  const bookname = await page.$eval('.book-name', el => el.innerText.trim());
  // 状态
  const state = await page.$eval('.state', el => el.innerText);
  // 分类
  const catogry = await page.$eval('.label', el => el.innerText);
  // 关键字
  const keys = await page.$$eval('.book-label span a', els => els.map(v => v.innerText));
  // 书籍数据
  const nums = await page.$$eval('.nums span', els => {
    // 解析中文数字
    function parseNumText(wordCount) {
      return wordCount.includes('万') ? parseFloat(wordCount) * 10000 : parseFloat(wordCount);
    }
    // 字数
    const wordCount = parseNumText(els[0].querySelector('i').innerText);
    // 总推荐
    const recommendCount = parseNumText(els[1].querySelector('i').innerText);
    // 总点击
    const clickCount = parseNumText(els[2].querySelector('i').innerText);
    // 周推荐
    const weekRecommendCount = parseNumText(els[3].querySelector('i').innerText);
    return {
      wordCount,
      recommendCount,
      clickCount,
      weekRecommendCount,
    };
  });
  // 书籍简介
  const bookDesc = await page.$eval('.book-dec > p', el => el.innerText);
  // 最新章节
  const latestChapter = await page.$eval('.tit a', el => el.innerText);
  // 最新章节-概述
  const latestChapterOverview = await page.$eval('.con', el => el.innerText);
  // 阅读地址
  const readUrl = await page.$eval('.read-btn', el => el.getAttribute('href'));
  // 作者名称
  const author = await page.$eval('.au-name a', el => el.innerText);
  // 作者头像
  const avatar = await page.$eval('.au-head a img', el => el.src);
  return {
    bookname,
    state,
    catogry,
    keys,
    ...nums,
    bookDesc,
    latestChapter,
    latestChapterOverview,
    readUrl,
    author,
    avatar
  };
}

// 插入完整数据
async function getBookData(url, page) {
  const {
    bookname,
    state,
    catogry,
    keys,
    wordCount = nums.wordCount,
    recommendCount = nums.recommendCount,
    clickCount = nums.clickCount,
    weekRecommendCount = nums.weekRecommendCount,
    bookDesc,
    latestChapter,
    latestChapterOverview,
    readUrl,
    author,
    avatar
  } = await getDetail(url, page);
  const values = [
    bookname,
    state,
    catogry,
    keys.toString(),
    wordCount,
    recommendCount,
    clickCount,
    weekRecommendCount,
    bookDesc,
    latestChapter,
    latestChapterOverview,
    readUrl,
    author
  ];
  const authorInfo = [author, avatar]
  await DBOP.insertBookDetail(values);
  await DBOP.insertAuthorBook(authorInfo);
  console.log('=========《' + bookname + '》-------详情-----------成功落库');
}

// 更新书籍读书地址
async function updateReadUrl(page, bookName, detailUrl) {
  const { readUrl } = await getDetail(detailUrl, page);
  await DBOP.updateReadUrl([readUrl, bookName]);
}
